//
//  HWMineViewController.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/22.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit
//import LYEmptyView
class HWMineViewController: HWBaseViewController {
    
    var collectionView:UICollectionView!
    var collectionProtocol = HWMineCollectionProtocol()
    var httpManager = HWMineHttpManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        addAction()
        
        
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
    
}

extension HWMineViewController {
    private func initUI() -> Void {
        navigationView.isHidden = true
 
        let flowL = UICollectionViewFlowLayout.init()
        flowL.scrollDirection = .vertical
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.backgroundColor = .clear
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(HWMineInfoCell.self, forCellWithReuseIdentifier: "HWMineInfoCell")
        collectionView.register(HWMineItemCell.self, forCellWithReuseIdentifier: "HWMineItemCell")
        collectionView.register(HWMineHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HWMineHeaderView")
        view.addSubview(collectionView)
        collectionView.contentInsetAdjustmentBehavior = .never
        
        
        
    }
    
    private func initFrame() -> Void {
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension HWMineViewController {
    func loadData() -> Void {
        httpManager.getMineHomeInfo { model in
            UserDefaults.standard.set(model.user.face, forKey: "userHeadImage")
            HWLocallyStoredData.setUserName(model.user.user_name)
            HWLocallyStoredData.setUserFace(model.user.face)
//            if model.user.vip == "0" {
//                self.isVip = false
//            } else {
//                self.isVip = true
//            }
            self.collectionProtocol.model = model
            self.collectionView.reloadData()
        } failure: { error in

        }

//        httpManager.getMineNewMessageInfo { show in
//            self.tableViewProtocol.showBadge = true
//            self.tableView.reloadData()
//        } failure: { error in
//            self.tableViewProtocol.showBadge = false
//            self.tableView.reloadData()
//        }

        
    }
}
extension HWMineViewController {
    func addAction() -> Void {
//        /// 设置
//        self.tableViewProtocol.settingButtonClick = {
//            self.jumpTo(type: "306", urlString: "")
//        }
//        /// 消息
//        self.tableViewProtocol.messageButtonClick = {
//            let message = HWXiaoXiMessageCenterViewController()
//            self.navigationController?.pushViewController(message, animated: true)
//        }
        /// 点击头像
        self.collectionProtocol.headButtonClick = {
            if HWLocallyStoredData.getLoginUserID().count>0{
                let mineInfo = HWPersonalDataViewController()
                self.navigationController?.pushViewController(mineInfo, animated: true)
            }else {
                let loginController = HWLoginController()
                self.navigationController?.pushViewController(loginController, animated: true)
            }

        }
        
        self.collectionProtocol.clickItem = {
            index,userModel in
            if index == 0 {
                if HWLocallyStoredData.getLoginUserID().count>0{
                    let saveCard = HWSaveMoneyCardController()
                    self.navigationController?.pushViewController(saveCard, animated: true)
                } else {
                    let loginController = HWLoginController()
                    self.navigationController?.pushViewController(loginController, animated: true)
                }
            } else {
                if HWLocallyStoredData.getLoginUserID().count>0{
                    if userModel.is_huiyuan == true {
                        self.navigationController?.pushViewController(HWVipCenterController(), animated: true)
                    } else {
                        let openVipController = HWVIPController()
                        self.navigationController?.pushViewController(openVipController, animated: true)
                    }
                    
                }else{
                    let loginController = HWLoginController()
                    self.navigationController?.pushViewController(loginController, animated: true)
                }
            }
            
        }

        /// 签到
        self.collectionProtocol.signButtonClick = {
            if HWLocallyStoredData.getLoginUserID().count>0{
                let sign = HWNewSignInViewController()
                self.navigationController?.pushViewController(sign, animated: true)
            }else{
                let loginController = HWLoginController()
                self.navigationController?.pushViewController(loginController, animated: true)
            }

        }

        /// 卡券
        self.collectionProtocol.cardButtonClick = {
            if HWLocallyStoredData.getLoginUserID().count>0{
                let mycard = HWMyVouchersViewController()
                self.navigationController?.pushViewController(mycard, animated: true)
            }else{
                let loginController = HWLoginController()
                self.navigationController?.pushViewController(loginController, animated: true)
            }
        }
        /// 积分
        self.collectionProtocol.scoreButtonClick = {
            if HWLocallyStoredData.getLoginUserID().count>0{
                let sign = HWTaskCenterViewController()
                self.navigationController?.pushViewController(sign, animated: true)
            }else{
                let loginController = HWLoginController()
                self.navigationController?.pushViewController(loginController, animated: true)
            }
        }
        /// 钱包
        self.collectionProtocol.coinButtonClick = {
            if HWLocallyStoredData.getLoginUserID().count>0{
                let webViewTopUp = XMGWebViewController()
                webViewTopUp.isPay = true;
                self.navigationController?.pushViewController(webViewTopUp, animated: true)
            }else{
                let loginController = HWLoginController()
                self.navigationController?.pushViewController(loginController, animated: true)
            }
        }
//
//        /// 我的游戏
//        self.tableViewProtocol.myGameButtonClick = {
////            if HWLocallyStoredData.getLoginUserID().count>0{
////                let sign = HWNewSignInViewController()
////                self.navigationController?.pushViewController(sign, animated: true)
////            }else{
////                let loginController = HWLoginController()
////                self.navigationController?.pushViewController(loginController, animated: true)
////            }
//        }
//
//        /// 我的关注
//        self.tableViewProtocol.myFollowButtonClick = {
//            if HWLocallyStoredData.getLoginUserID().count>0{
//                let webViewTopUp = HWWebViewTopUpController()
//                self.navigationController?.pushViewController(webViewTopUp, animated: true)
//            }else{
//                let loginController = HWLoginController()
//                self.navigationController?.pushViewController(loginController, animated: true)
//            }
//        }
//
//        /// 开通VIP
//        self.tableViewProtocol.openVipButtonClick = {
//            if HWLocallyStoredData.getLoginUserID().count>0{
//                if self.isVip == true {
//                    self.navigationController?.pushViewController(HWVipCenterController(), animated: true)
//                } else {
//                    let openVipController = HWOpenVipController()
//                    self.navigationController?.pushViewController(openVipController, animated: true)
//                }
//
//            }else{
//                let loginController = HWLoginController()
//                self.navigationController?.pushViewController(loginController, animated: true)
//            }
//
//        }
        /// 功能
        self.collectionProtocol.navClick = {
            mod in
            if mod.login == "1" {
                if HWLocallyStoredData.getLoginUserID().count>0{
                    self.jumpTo(type: mod.type,urlString: mod.url)
                }else{
                    let loginController = HWLoginController()
                    self.navigationController?.pushViewController(loginController, animated: true)
                }
            }else {
                if mod.type == "0" {
                    let detail = HWGameDetailController()
                    detail.gameId = mod.gid
                    self.navigationController?.pushViewController(detail, animated: true)
                } else {
                    self.jumpTo(type: mod.type,urlString: mod.url)
                }
                
            }


        }
//        ///更多
//        self.tableViewProtocol.moreButtonClick = {
//
//        }
//        /// 点击游戏
//        self.tableViewProtocol.itemButtonClick = {
//            id in
//            let gameVC = HWGameDetailController()
//            gameVC.gameId = id
//            self.navigationController?.pushViewController(gameVC, animated: true)
//        }
    }
    
    func jumpTo(type:String,urlString:String) -> Void {
        HWUniversal.pushController(type: type, urlString: urlString)
    }
    
}
